package com.example.lin.guidancedemo.adpater;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.lin.guidancedemo.R;

public class GuidancePagerAdpater extends PagerAdapter {


    private static final String TAG = "GuidancePagerAdpater";

    private int[] imageResIds;
    private Context context;

    public GuidancePagerAdpater(int[] imageResIds, Context context) {
        this.imageResIds = imageResIds;
        this.context = context;
    }

    @Override
    public int getCount() {
        return imageResIds.length;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    /**
     * 加载视图
     *
     * @param container ViewPager的容器
     * @param position  当前页面的下标
     * @return 需要显示的页面
     */
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        //加载每个页面需要显示的布局
        View pageItem = LayoutInflater.from(context).inflate(R.layout.view_guidance_page_item, container, false);
        ImageView iv_show = pageItem.findViewById(R.id.iv_show);
        iv_show.setImageResource(imageResIds[position]);

        //将 加载的页面添加到 ViewPager容器中
        container.addView(pageItem);
        Log.d(TAG, "instantiateItem: " + position+":"+container.getChildCount());

        return pageItem;
    }

    /**
     * 移除视图
     *
     * @param container ViewPager的容器
     * @param position  需要移除的下标
     * @param object    当前页面视图的对象
     */
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
        Log.i(TAG, "destroyItem: " + position+":"+container.getChildCount());
    }

}
